package com.ruoyi.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GlobalInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysSerialRuleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pms.mapper.PmsTaskMapper;
import com.ruoyi.pms.domain.PmsTask;
import com.ruoyi.pms.service.IPmsTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务信息Service业务层处理
 *
 * @author huikai
 * @date 2025-03-18
 */
@Service
public class PmsTaskServiceImpl extends ServiceImpl<PmsTaskMapper, PmsTask> implements IPmsTaskService {
    @Autowired
    private PmsTaskMapper pmsTaskMapper;

    @Autowired
    private PmsTaskLogServiceImpl pmsTaskLogService;

    @Autowired
    private SysSerialRuleServiceImpl sysSerialRuleService;

    /**
     * 查询任务信息列表
     *
     * @param pmsTask 任务信息
     * @return 任务信息
     */
    @Override
    public List<PmsTask> selectPmsTaskList(PmsTask pmsTask) {
        return baseMapper.selectList(new QueryWrapper<>(pmsTask));
        //return pmsTaskMapper.selectPmsTaskList(pmsTask);
    }


    /**
    * 查询任务信息记录
    * @param taskId
    * @return
    */
    @Override
    public PmsTask getInfoByTaskId(String taskId) {
        return pmsTaskMapper.selectById(taskId);
    }

    /**
     * 新增任务信息记录
     * @param pmsTask 任务信息
     * @return
     */
    @Override
    public Boolean add(PmsTask pmsTask) {
        if (StringUtils.isBlank(pmsTask.getTaskId())) {
            pmsTask.setTaskId(sysSerialRuleService.generateSerialNumber("pms_task_id"));
        }
        pmsTask.setCreateBy(GlobalInfo.getUserId());
        pmsTask.setCreateTime(new Date());
        return pmsTaskMapper.insert(pmsTask) > 0;
    }

    /**
     * 修改任务信息记录
     * @param pmsTask 任务信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(PmsTask pmsTask) {
        pmsTaskLogService.createTaskLogByTaskList(Collections.singletonList(pmsTask),"修改");
        pmsTask.setTaskVersion(pmsTask.getTaskVersion() + 1);
        pmsTask.setUpdateBy(GlobalInfo.getUserId());
        pmsTask.setUpdateTime(new Date());
        return pmsTaskMapper.updateById(pmsTask) > 0;
    }

    /**
     * 删除任务信息记录
     * @param taskIds
     * @return
     */
    @Override
    public Boolean removeByTaskIds(String[] taskIds) {
        QueryWrapper<PmsTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(PmsTask::getTaskId,taskIds);
        List<PmsTask> pmsTaskList = pmsTaskMapper.selectList(queryWrapper);
        pmsTaskLogService.createTaskLogByTaskList(pmsTaskList,"删除");
        return pmsTaskMapper.delete(queryWrapper) > 0;
    }
}